const fileUtil = {
  getFiles() {
    let files = null
    try {
      files = JSON.parse(wx.getStorageSync('local-files'))
    } catch (error) {
      files = []
    }
    let fs = wx.getFileSystemManager()
    return files.filter((file) => {
      let access = true
      try {
        fs.accessSync(file.url)
      } catch (e) {
        console.error(e)
        access = false
      }
      return access
    })
  },
  saveFiles(files) {
    wx.setStorageSync('local-files', JSON.stringify(files))
  },
  getFilesByLocalId(localId) {
    let files = null
    try {
      files = JSON.parse(wx.getStorageSync('local-files'))
    } catch (error) {
      files = []
    }
    let fs = wx.getFileSystemManager()
    return files.filter((file) => {
      let access = true
      try {
        fs.accessSync(file.url)
      } catch (e) {
        console.error(e)
        access = false
      }
      return file.localId == localId && access
    })
  },
  getNotUploadCount(localId) {
    return fileUtil.getFilesByLocalId(localId).length
  },
  saveFilesByLocalId(localId, files) {
    let oldFiles = fileUtil.getFiles()
    let newFiles = oldFiles.filter((file) => {
      return file.localId != localId
    })
    let resFiles = newFiles.concat(files)
    wx.setStorageSync('local-files', JSON.stringify(resFiles))
  },
}
export default fileUtil